﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.EHR.Library;
using ZX.BLL.Extend;
using ZX.DBModule;
using ZX.Web.UI.Controls;
using ZX.Common;
using ZX.DAL.DBExtend;
using ZX.CRM.Bussiness;
using ZX.SYS.Library;
using ZX.BLL;
using ZX.CRM.Library;
using System.Data;
using System.Data.SqlClient;

namespace ZX.WebApp.EditWidows.OrganizationalManager
{
    public partial class DataProcessin : BasePage
    {
        //职务类型
        private const string Const_DutyInfoType = "D9CFA189-7236-476F-8D32-E716D60B60C4";
        //职级
        private const string Const_DutyLevel = "C2D716B1-CA5D-410C-AA3E-B39A466DFAB2";
        //职等
        private const string Const_DutyGrade = "2C7799F8-1904-4D19-BEDF-9843549AFE33";

        BDatPhraseDetail bc = new BDatPhraseDetail();
        BaseBussnies bll = new BaseBussnies();
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = this.GetStringFromQueryString("action", "");
            switch (action.ToLower())
            {
                case "get":
                    GetData();
                    break;
                case "insert":
                    InsertData();
                    break;
                case "update":
                    UpdateDate();
                    break;
                case "delete":
                    DeleteData();
                    break;
                default:
                    InitInfo();
                    break;
            }
        }
        private void GetData()
        {
            string entity = this.GetStringFromQueryString("entity", "");
            switch (entity.ToLower())
            {
                case "dutytype": GetDutyType();
                    break;
                case "dutyinfo": GetDutyInfoByDutyTypeId();
                    break;
            }
        }
        private void GetDutyType()
        {
            BDatPhraseDetail datPhrase = new BDatPhraseDetail();
            IList<DatPhraseDetail> dutyType = datPhrase.GetDatPhraseDetailByClassValue("DutyInfoType");
            string listjson = Globals.getJSON<DatPhraseDetail>(dutyType);
            HttpContext.Current.Response.Write(listjson);

        }
        private void GetDutyInfoByDutyTypeId()
        {
            string dutyTypeId = this.GetStringFromQueryString("dutyTypeId", "");
            BDutyInfo dutyMgr = new BDutyInfo();
            IList<V_DatDutyInfo> dutyType = dutyMgr.GetDutyInfoByDutyId(new Guid(dutyTypeId));
            string listjson = Globals.getJSON<V_DatDutyInfo>(dutyType);
            HttpContext.Current.Response.Write(listjson);
        }


        private void InsertData()
        {
            string entity = this.GetStringFromQueryString("entity", "");
            string strJson = this.GetStringFromQueryString("json", "");
            switch (entity)
            {
                case "DatDutyType":
                    InsertDutyType(strJson);
                    break;
                case "DatDutyLevel":
                    InsertDutyLevel(strJson);
                    break;
                case "DatDutyGrade":
                    InsertDutyGrade(strJson);
                    break;
                case "DatDutyInfo":
                    InsertDutyInfo(strJson);
                    break;
                case "DatPostDutygrade":
                    InsertDatDutyInfoAndDutyGrade(strJson);
                    break;
                case "CrmInnerComment":
                    InsertComment(strJson);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 插入职务类型
        /// </summary>
        private void InsertDutyType(string strJson)
        {
            try
            {

                V_DatDutyType instance = (V_DatDutyType)Globals.JSonToObject<V_DatDutyType>(strJson);
                if (bc.GetDatPhraseDetailByPhrasaeTextAndClassId(instance.PhraseText, new Guid(Const_DutyInfoType), new Guid("00000000-0000-0000-0000-000000000000"), 0))
                {
                    HttpContext.Current.Response.Write("该职务类型名称已经存在!");
                    return;
                }
                else
                {
                    string strSql = string.Format("insert into DatPhraseDetail(PhraseText,PhraseValue,ClassID,IsPause,IsLock,Seq,IsDefault) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                                                instance.PhraseText, instance.Seq, Const_DutyInfoType, "False", "False", instance.Seq, "False");
                    int iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("保存失败!");
                    }
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("保存失败!");
            }
        }
        /// <summary>
        /// 插入职等
        /// </summary>
        /// <param name="strJson"></param>
        private void InsertDutyLevel(string strJson)
        {
            try
            {
                V_DatDutyLevel instance = (V_DatDutyLevel)Globals.JSonToObject<V_DatDutyLevel>(strJson);
                if (bc.GetDatPhraseDetailByPhrasaeTextAndClassId(instance.PhraseText, new Guid(Const_DutyLevel), new Guid("00000000-0000-0000-0000-000000000000"), 0))
                {
                    HttpContext.Current.Response.Write("该职等名称已经存在!");
                    return;
                }
                else
                {
                    string strSql = string.Format("insert into DatPhraseDetail(PhraseText,PhraseValue,ClassID,IsPause,IsLock,Seq,IsDefault) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                                                instance.PhraseText, instance.Seq, Const_DutyLevel, "False", "False", instance.Seq, "False");
                    int iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("保存失败!");
                    }
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("保存失败!");
            }
        }
        /// <summary>
        /// 插入职级
        /// </summary>
        /// <param name="strJson"></param>
        private void InsertDutyGrade(string strJson)
        {
            try
            {
                V_DatDutyGrade instance = (V_DatDutyGrade)Globals.JSonToObject<V_DatDutyGrade>(strJson);
                if (bc.GetDatPhraseDetailByPhrasaeTextAndClassId(instance.PhraseText, new Guid(Const_DutyGrade), new Guid("00000000-0000-0000-0000-000000000000"), 0))
                {
                    HttpContext.Current.Response.Write("该职级名称已经存在!");
                    return;
                }
                else
                {
                    string strSql = string.Format("insert into DatPhraseDetail(PhraseText,PhraseValue,ClassID,IsPause,IsLock,Seq,IsDefault) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                                                instance.PhraseText, instance.Seq, Const_DutyGrade, "False", "False", instance.Seq, "False");
                    int iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                        return;
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("保存失败!");
                        return;
                    }
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("保存失败!");
            }
        }
        /// <summary>
        /// 插入评论信息
        /// </summary>
        /// <param name="strJson"></param>
        private void InsertComment(string strJson)
        {
            try
            {
                CrmInnerComment instance = (CrmInnerComment)Globals.JSonToObject<CrmInnerComment>(strJson);
                BaseBussnies bll = new BaseBussnies();
                if (instance != null)
                {
                    string strSql = string.Format("insert into CrmInnerComment(InnerSumID,TeachID,ComContent) values('{0}','{1}','{2}')",
                                                    instance.InnerSumID, instance.TeachID, instance.ComContent);
                    int iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                        return;
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("发表失败!");
                        return;
                    }
                }
                else
                {
                    HttpContext.Current.Response.Write("发表失败!");
                    return;
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex);
            }
        }
        /// <summary>
        /// 插入职务信息
        /// </summary>
        /// <param name="strJson"></param>
        private void InsertDutyInfo(string strJson)
        {
            try
            {
                DatDutyInfo instance = (DatDutyInfo)Globals.JSonToObject<DatDutyInfo>(strJson);
                BaseBussnies bll = new BaseBussnies();
                BDutyInfo bc = new BDutyInfo();
                if (bc.IsExistByName(instance.Name, instance.DutyTypeId))
                {
                    HttpContext.Current.Response.Write("该职务名称已存在!");
                    return;
                }
                else
                {
                    string strSql = string.Format("insert into DatDutyInfo(DutyTypeId,SequenceNo,Name,Remark) values('{0}','{1}','{2}','{3}')",
                                                instance.DutyTypeId, instance.SequenceNo, instance.Name, instance.Remark);
                    int iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                        return;
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("保存失败!");
                        return;
                    }
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("保存失败!");
            }
        }
        /// <summary>
        /// 职务对应职级
        /// </summary>
        /// <param name="strJson"></param>
        private void InsertDatDutyInfoAndDutyGrade(string strJson)
        {
            try
            {
                DatPostDutygrade instance = (DatPostDutygrade)Globals.JSonToObject<DatPostDutygrade>(strJson);
                DbSql db = new DbSql();
                string strSql = "";
                if (instance.ID.ToString() == "00000000-0000-0000-0000-000000000000")
                {
                    if (instance.DutyGradeId.ToString() == "00000000-0000-0000-0000-000000000000")
                    {
                        string sql = string.Format("select COUNT(*) from DatPostDutygrade where PostID='{0}'", instance.PostID);
                        int i = db.ExecuteScalar(sql);
                        if (i > 0)
                        {
                            strSql = string.Format("delete DatPostDutygrade where PostID='{0}'",
                                    instance.PostID);
                        }
                        else
                        {
                            HttpContext.Current.Response.Write("1");
                            return;
                        }
                    }
                    else
                    {
                        strSql = string.Format("insert into DatPostDutygrade(PostID,DutyGradeId) values('{0}','{1}')",
                                instance.PostID, instance.DutyGradeId);
                    }
                }
                else
                {
                    strSql = string.Format("update DatPostDutygrade set PostID='{0}',DutyGradeId='{1}' where ID='{2}'",
                                            instance.PostID, instance.DutyGradeId, instance.ID);
                }
                if (db.RunSqlCommand(strSql) > 0)
                {
                    HttpContext.Current.Response.Write("1");
                    return;
                }
                else
                {
                    HttpContext.Current.Response.Write("保存失败!");
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex);
            }
        }



        private void UpdateDate()
        {
            string entity = this.GetStringFromQueryString("entity", "");
            string strJson = this.GetStringFromQueryString("json", "");
            switch (entity)
            {
                case "DatDutyType":
                    UpdateDutyType(strJson);
                    break;
                case "DatDutyLevel":
                    UpdateDutyLevel(strJson);
                    break;
                case "DatDutyGrade":
                    UpdateDutyGrade(strJson);
                    break;
                case "DatDutyInfo":
                    UpdateDutyInfo(strJson);
                    break;
                //英才学员结业，班级
                case "CrmMajorStudentHr":
                    UpdateMajorStudentHr(strJson);
                    break;
                case "CrmPay":
                    UpdateCrmPay(strJson);
                    break;
                case "CrmCheckPay":
                    UpdateCrmCheckPay(strJson);
                    break;
                case "CrmPayInvoice":
                    UpdateCrmPayInvoice(strJson);
                    break;
                case "CrmPayCheck":
                    UpdateCrmPayCheck(strJson);
                    break;
                case "DatPost":
                    string strPositionId = this.GetStringFromQueryString("manager", "");
                    string strDeptId = this.GetStringFromQueryString("deptid", "");
                    if (this.GetStringFromQueryString("cancel", "") == "0")
                    {
                        UpdateManagerPositions(strPositionId, strDeptId);
                    }
                    else
                    {
                        UpdateManagerPosition(strPositionId, strDeptId);
                    }
                    break;
                case "DatPostDuty":
                    string positionid = this.GetStringFromQueryString("positionid", "");
                    string postid = this.GetStringFromQueryString("postid", "");
                    UpdateDatPostDuty(postid, positionid);
                    break;
                case "CrmCommision":
                    string OrderProductId = this.GetStringFromQueryString("OrderProductId", "");
                    string UserId = this.GetStringFromQueryString("UserId", "");
                    string CommissionTime = this.GetStringFromQueryString("CommissionTime", "");
                    SaveCommision(OrderProductId, UserId, CommissionTime);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 计算佣金基础表
        /// </summary>
        /// <param name="Commision">佣金所属月份</param>
        private void SaveCommision(string orderproductid,string  userid,string Commisiondate)
        {
            BCrmPayFlow cr = new BCrmPayFlow();
            Guid UserId = new Guid(userid.ToString());
            string returnstr = cr.ExecCrmCommisionInfo(orderproductid,UserId, Convert.ToDateTime(Commisiondate));

            if (returnstr.Trim().Length <1)
                 HttpContext.Current.Response.Write("1");
            else
                HttpContext.Current.Response.Write(returnstr);
                  

        }
        /// <summary>
        /// 更新职务类型
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateDutyType(string strJson)
        {
            try
            {
                V_DatDutyType instance = (V_DatDutyType)Globals.JSonToObject<V_DatDutyType>(strJson);
                string strId = this.GetStringFromQueryString("id", "");
                if (bc.GetDatPhraseDetailByPhrasaeTextAndClassId(instance.PhraseText, new Guid(Const_DutyInfoType), new Guid(strId), 1))
                {
                    HttpContext.Current.Response.Write("该职务类型名称已经存在!");
                    return;
                }
                else
                {
                    string strSql = string.Format("update DatPhraseDetail set PhraseText='{0}',PhraseValue='{1}',Seq='{2}' where PhraseID='{3}'",
                                   instance.PhraseText, instance.Seq, instance.Seq, strId);
                    int iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("修改失败!");
                    }
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("修改失败!");
            }
        }
        /// <summary>
        /// 更新职等类型
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateDutyLevel(string strJson)
        {
            try
            {
                V_DatDutyLevel instance = (V_DatDutyLevel)Globals.JSonToObject<V_DatDutyLevel>(strJson);
                string strId = this.GetStringFromQueryString("id", "");
                if (bc.GetDatPhraseDetailByPhrasaeTextAndClassId(instance.PhraseText, new Guid(Const_DutyLevel), new Guid(strId), 1))
                {
                    HttpContext.Current.Response.Write("该职等名称已经存在!");
                    return;
                }
                else
                {
                    string strSql = string.Format("update DatPhraseDetail set PhraseText='{0}',PhraseValue='{1}',Seq='{2}' where PhraseID='{3}'",
                                   instance.PhraseText, instance.Seq, instance.Seq, strId);
                    int iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("修改失败!");
                    }
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("修改失败!");
            }
        }
        /// <summary>
        /// 更新职级类型
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateDutyGrade(string strJson)
        {
            try
            {
                V_DatDutyGrade instance = (V_DatDutyGrade)Globals.JSonToObject<V_DatDutyGrade>(strJson);
                string strId = this.GetStringFromQueryString("id", "");
                if (bc.GetDatPhraseDetailByPhrasaeTextAndClassId(instance.PhraseText, new Guid(Const_DutyGrade), new Guid(strId), 1))
                {
                    HttpContext.Current.Response.Write("该职级名称已经存在!");
                    return;
                }
                else
                {
                    string strSql = string.Format("update DatPhraseDetail set PhraseText='{0}',PhraseValue='{1}',Seq='{2}' where PhraseID='{3}'",
                                   instance.PhraseText, instance.Seq, instance.Seq, strId);
                    int iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                        return;
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("修改失败!");
                        return;
                    }
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("修改失败!");
            }
        }

        /// <summary>
        /// 修改学员状态，班级               
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateMajorStudentHr(string strJson)
        {
            try
            {
                int iResult = 0;
                IDataParameter[] _Parameters = new IDataParameter[] { };
                DbSql db = new DbSql();

                string strSql = "";
                string StrId = GetStringFromQueryString("StrClassId", "");
                string StrClassId = GetStringFromQueryString("ClassId", "");
                if (StrId != "")
                {
                    if (StrId.Contains(','))
                    {
                        string[] arr = StrId.Split(',');
                        for (int i = 0; i < arr.Length; i++)
                        {
                            strSql = string.Format("update CrmMajorStudentHr set ClassID='{0}' where ID='{1}'", StrClassId, arr[i]);
                            iResult = bll.RunSqlCommand(strSql);
                        }
                    }
                    else
                    {
                        strSql = string.Format("update CrmMajorStudentHr set ClassId='{0}' where ID='{1}'", StrClassId, StrId);
                        iResult = bll.RunSqlCommand(strSql);
                    }
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                        return;
                    }
                }
                else
                {
                    string id = GetStringFromQueryString("ID", "");
                    if (id != "" && id.Contains(','))
                    {
                        string[] arr = id.Split(',');
                        for (int i = 0; i < arr.Length; i++)
                        {
                            strSql = string.Format("update CrmMajorStudentHr set StudentState={0},GraduateDate='{1}',Review='{2}' where ID='{3}'",
                                                    GetStringFromQueryString("StudentState", ""), GetStringFromQueryString("GraduateDate", ""), GetStringFromQueryString("Review", ""), arr[i]);
                            if (GetStringFromQueryString("StudentState", "") == "4")
                            {
                                strSql += " update CrmDeliverHrTerm set TermExistNum=TermExistNum-1 where ID=(select TermId from CrmDeliverHr where ID=(select MajorID from CrmMajorStudentHr where ID='" + arr[i] + "'))";
                            }
                            iResult = bll.RunSqlCommand(strSql);
                            if (iResult > 0)
                            {
                                SqlParameter StudentHrId = new SqlParameter("@StudentHrId", SqlDbType.UniqueIdentifier, 50);
                                StudentHrId.Direction = ParameterDirection.Input;
                                StudentHrId.Value = arr[i] == "" ? Guid.Empty : new Guid(arr[i]);
                                SqlParameter ReturnValue = new SqlParameter("@ReturnValue", SqlDbType.NVarChar, 50);
                                ReturnValue.Direction = ParameterDirection.Output;
                                _Parameters = new IDataParameter[] { StudentHrId, ReturnValue };
                                db.RunProcedureReturnString("sp_UpdateOrderByDeliverHr", _Parameters, out iResult);
                            }
                            else
                            {
                                HttpContext.Current.Response.Write("操作失败!");
                                return;
                            }
                        }
                        HttpContext.Current.Response.Write("1");
                        return;
                    }
                    else
                    {
                        strSql = string.Format("update CrmMajorStudentHr set StudentState={0},GraduateDate='{1}',Review='{2}' where ID='{3}'",
                                                    GetStringFromQueryString("StudentState", ""), GetStringFromQueryString("GraduateDate", ""), GetStringFromQueryString("Review", ""), GetStringFromQueryString("ID", ""));
                        if (GetStringFromQueryString("StudentState", "") == "4")
                        {
                            strSql += " update CrmDeliverHrTerm set TermExistNum=TermExistNum-1 where ID=(select TermId from CrmDeliverHr where ID=(select MajorID from CrmMajorStudentHr where ID='" + GetStringFromQueryString("ID", "") + "'))";
                        } 
                        iResult = bll.RunSqlCommand(strSql);
                        if (iResult > 0)
                        {
                            SqlParameter StudentHrId = new SqlParameter("@StudentHrId", SqlDbType.UniqueIdentifier, 50);
                            StudentHrId.Direction = ParameterDirection.Input;
                            StudentHrId.Value = GetStringFromQueryString("ID", "") == "" ? Guid.Empty : new Guid(GetStringFromQueryString("ID", ""));
                            SqlParameter ReturnValue = new SqlParameter("@ReturnValue", SqlDbType.NVarChar, 50);
                            ReturnValue.Direction = ParameterDirection.Output;
                            _Parameters = new IDataParameter[] { StudentHrId, ReturnValue };
                            db.RunProcedureReturnString("sp_UpdateOrderByDeliverHr", _Parameters, out iResult);


                            HttpContext.Current.Response.Write("1");
                            return;
                        }
                        else
                        {
                            HttpContext.Current.Response.Write("操作失败!");
                            return;
                        }
                    }
                        
                       
                        
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("操作失败!");
            }
        }
        /// <summary>
        /// 佣金操作               
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateCrmPay(string strJson)
        {
            try
            {
                int iResult = 0;
                IDataParameter[] _Parameters = new IDataParameter[] { };
                DbSql db = new DbSql();
                string strSql = string.Format("update CrmPay set CommissionDate='{0}',CommissionUserID='{1}',CommissionTime='{2}',CommissionPay='{3}',CommissionState={4} where ID='{5}'",
                                            DateTime.Now.ToShortDateString(), UserId, GetStringFromQueryString("CommissionTime", ""), GetStringFromQueryString("CommissionPay", ""), GetStringFromQueryString("CommissionState", ""), GetStringFromQueryString("ID", ""));

                iResult = bll.RunSqlCommand(strSql);
                if (iResult > 0)
                {
                    HttpContext.Current.Response.Write("1");
                    return;
                }
                else
                {
                    HttpContext.Current.Response.Write("操作失败!");
                    return;
                }

            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex);
            }
        }
        /// <summary>
        /// 复核操作               
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateCrmCheckPay(string strJson)
        {
            try
            {
                int iResult = 0;
                IDataParameter[] _Parameters = new IDataParameter[] { };
                DbSql db = new DbSql();
                string strSql = string.Format("update CrmPay set AgainCheckTime='{0}',AgainCheckUser='{1}',IsAgainCheck='{2}' where ID='{3}'",
                                            DateTime.Now.ToShortDateString(), UserId, GetStringFromQueryString("CheckValue", ""),GetStringFromQueryString("ID", ""));

                iResult = bll.RunSqlCommand(strSql);
                if (iResult > 0)
                {
                    HttpContext.Current.Response.Write("1");
                    return;
                }
                else
                {
                    HttpContext.Current.Response.Write("操作失败!");
                    return;
                }

            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex);
            }
        }
        /// <summary>
        /// 财务审核               
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateCrmPayInvoice(string strJson)
        {
            try
            {
                int iResult = 0;
                IDataParameter[] _Parameters = new IDataParameter[] { };
                DbSql db = new DbSql();
                string strSql = "";
                if (GetStringFromQueryString("view", "") == "check")
                {
                    strSql = string.Format("update CrmProductPay set CheckAddTime='{0}',CheckAddUser='{1}',IsFifteenMonth='{2}',IsProjectOver='{3}',IsCEOInnerTrain={4},IsInnerTrainSian='{5}',IsFisrtSale='{6}',IsCheck='1',IsSelfHelp='{7}',SelfHelpYear='{8}',IsPEContinu='{9}' where ID='{10}' update CrmCustomerOrder set ProductPackID='{11}' where ID='{12}'",
                                            DateTime.Now.ToShortDateString(), UserId, GetStringFromQueryString("IsFifteenMonth", ""), GetStringFromQueryString("IsProjectOver", ""), GetStringFromQueryString("IsCEOInnerTrain", ""), GetStringFromQueryString("IsInnerTrainSian", ""),
                                            GetStringFromQueryString("IsFisrtSale", ""), GetStringFromQueryString("IsSelfHelp", ""), GetStringFromQueryString("SelfHelpYear", ""), GetStringFromQueryString("IsPEContinu", ""), GetStringFromQueryString("ID", ""), GetStringFromQueryString("PackID", ""), GetStringFromQueryString("OrderID", ""));
                }
                else
                {
                    strSql = string.Format("update CrmPay set InvoiceAddTime='{0}',InvoiceAddUser='{1}',InvoiceMoney='{2}',InvoiceNum='{3}',Receipt='{4}',ExpressNo='{5}',IsFinish='1' where ID='{6}'",
                                                DateTime.Now.ToShortDateString(), UserId, GetStringFromQueryString("InvoiceMoney", ""), GetStringFromQueryString("Invoince", ""), GetStringFromQueryString("Receipt", ""),GetStringFromQueryString("ExpressNo",""), GetStringFromQueryString("ID", ""));
                }
                iResult = bll.RunSqlCommand(strSql);
                if (iResult > 0)
                {
                    HttpContext.Current.Response.Write("1");
                    return;
                }
                else
                {
                    HttpContext.Current.Response.Write("操作失败!");
                    return;
                }

            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex);
            }
        }
        /// <summary>
        /// 业绩审核               
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateCrmPayCheck(string strJson)
        {
            try
            {
                int iResult = 0;
                IDataParameter[] _Parameters = new IDataParameter[] { };
                DbSql db = new DbSql();
                string strSql = string.Format("update CrmPay set CommissionDate='{0}',CommissionUserID='{1}',CommissionTime='{2}',CommissionPay='{3}',CommissionState={4} where ID='{5}'",
                                            DateTime.Now.ToShortDateString(), UserId, GetStringFromQueryString("CommissionTime", ""), GetStringFromQueryString("CommissionPay", ""), GetStringFromQueryString("CommissionState", ""), GetStringFromQueryString("ID", ""));

                iResult = bll.RunSqlCommand(strSql);
                if (iResult > 0)
                {
                    HttpContext.Current.Response.Write("1");
                    return;
                }
                else
                {
                    HttpContext.Current.Response.Write("操作失败!");
                    return;
                }

            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex);
            }
        }
        /// <summary>
        /// 修改职务信息
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateDutyInfo(string strJson)
        {
            try
            {
                DatDutyInfo instance = (DatDutyInfo)Globals.JSonToObject<DatDutyInfo>(strJson);
                BaseBussnies bll = new BaseBussnies();
                BDutyInfo bllInfo = new BDutyInfo();
                if (bllInfo.IsExistByNameForUpdate(instance.Name, instance.ID, instance.DutyTypeId))
                {
                    HttpContext.Current.Response.Write("该职务名称已存在!");
                    return;
                }
                else
                {
                    string strSql = string.Format("update DatDutyInfo set SequenceNo='{0}',Name='{1}',Remark='{2}' where ID='{3}'",
                                                instance.SequenceNo, instance.Name, instance.Remark, instance.ID);
                    int iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                        return;
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("修改失败!");
                        return;
                    }
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("修改失败!");
            }
        }
        /// <summary>
        /// 设置职位负责人
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateManagerPosition(string positionId, string deptId)
        {
            try
            {
                BaseBussnies bll = new BaseBussnies();
                string strSql = string.Format("update DatDepartment set Manager='{0}' where DeptID='{1}'",
                                        positionId, deptId);
                int iResult = bll.RunSqlCommand(strSql);
                if (iResult > 0)
                {
                    strSql = string.Format("update DatPost set PostLevel=0 where PostID='{0}'",
                                       positionId);
                    iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                        return;
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("设置失败!");
                        return;
                    }
                }
                else
                {
                    HttpContext.Current.Response.Write("设置失败!");
                    return;
                }
                //}
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex);
            }
        }
        /// <summary>
        /// 取消职位负责人
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateManagerPositions(string positionId, string deptId)
        {
            try
            {
                BaseBussnies bll = new BaseBussnies();
                string strSql = string.Format("update DatDepartment set Manager='00000000-0000-0000-0000-000000000000' where DeptID='{0}'",
                                         deptId);
                int iResult = bll.RunSqlCommand(strSql);
                if (iResult > 0)
                {
                    strSql = string.Format("update DatPost set PostLevel=1 where PostID='{0}'", positionId);
                    iResult = bll.RunSqlCommand(strSql);
                    if (iResult > 0)
                    {
                        HttpContext.Current.Response.Write("1");
                        return;
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("设置失败!");
                        return;
                    }
                }
                else
                {
                    HttpContext.Current.Response.Write("设置失败!");
                    return;
                }
                //}
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex);
            }
        }
        /// <summary>
        ///  修改职位的职务
        /// </summary>
        /// <param name="strJson"></param>
        private void UpdateDatPostDuty(string postid, string positionid)
        {
            try
            {
                string strSql = string.Format("update DatPost set positionid='{0}' where PostID='{1}'",
                                     positionid, postid);
                int iResult = bll.RunSqlCommand(strSql);
                if (iResult > 0)
                {
                    HttpContext.Current.Response.Write("1");
                    return;
                }
                else
                {
                    HttpContext.Current.Response.Write("设置失败!");
                    return;
                }
                //}
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex);
            }
        }


        private void DeleteData()
        {
            string entity = this.GetStringFromQueryString("entity", "");
            string strJson = this.GetStringFromQueryString("json", "");
            switch (entity)
            {
                case "DatDutyType":
                case "DatDutyLevel":
                case "DatDutyGrade":
                    DeleteDutyData(strJson);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        private void DeleteDutyData(string Id)
        {
            try
            {
                BaseBussnies bll = new BaseBussnies();
                string strSql = string.Format("delete DatPhraseDetail where PhraseID='{0}'", Id);
                int iResult = bll.RunSqlCommand(strSql);
                if (iResult > 0)
                {
                    HttpContext.Current.Response.Write("1");
                }
                else
                {
                    HttpContext.Current.Response.Write("删除失败!");
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("删除失败!");
            }
        }




        public void InitInfo()
        {
            string id = this.GetStringFromQueryString("id", "");
            BaseBussnies bll = new BaseBussnies();
            DatDepartment department = new DatDepartment();
            bll.entity = department;
            department = (DatDepartment)bll.GetEntityById(id);
            BaseBussnies bllPostion = new BaseBussnies();
            DatPost postion = new DatPost();
            bllPostion.entity = postion;
            postion = (DatPost)bllPostion.GetEntityById(department.Manager.ToString());
            department.iSun_ManagerName = postion == null ? "未设置负责人" : postion.PostName;
            string json = department.ToJSON();
            HttpContext.Current.Response.Write(json);
        }
    }
}